package com.example.toutiao.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.toutiao.R;
import com.example.toutiao.db.DatabaseHelper;

public class ModifyPasswordActivity extends AppCompatActivity {

    private EditText etNewPassword;
    private Button btnSave;
    private String username;
    private DatabaseHelper db;

    @Override
    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        TextView tabBarTitle = findViewById(R.id.tabBarTitle);
        tabBarTitle.setText("修改密码");
        etNewPassword = findViewById(R.id.etNewPassword);
        btnSave = findViewById(R.id.btnSave);
        db = new DatabaseHelper(this);

        username = getIntent().getStringExtra("username");
        if (username == null) {
            Toast.makeText(this, "无效用户", Toast.LENGTH_SHORT).show();
            finish();
        }

        btnSave.setOnClickListener(v -> {
            String newPassword = etNewPassword.getText().toString().trim();
            if (!newPassword.isEmpty()) {
                db.updatePassword(username, newPassword);
                Toast.makeText(this, "密码已更新", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
